package com.quectel.mobile.controller.banner;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.banner.dto.BannerDto;
import com.quectel.core.module.banner.service.BannerService;
import com.quectel.core.module.notice.dto.NoticeDto;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.mobile.controller.BaseController;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yubobo
 * @email bobo.yu@quectel.com
 * @date 2021/12/23 3:50 下午
 */
@RestController
@RequestMapping("banner")
@Tag(name = "BannerController", description = "移动端banner资源相关api")
public class BannerController extends BaseController {

    public static final String VILLAGE_ID = "villageId";

    @DubboReference
    private BannerService bannerService;
    @DubboReference
    private VillageService villageService;

    @GetMapping("list")
    @Operation(summary = "移动端banner资源的分页查询列表")
    public Response<Page<BannerDto>> list(
            @Parameter(description = "小区 villageId") @RequestParam Long villageId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(VILLAGE_ID, villageId);
        Request request = Request.configParams(params);

        VillageDto villageDto = villageService.selectCacheById(villageId);
        if (villageDto != null) {
            params.put(LambdaUtil.getFieldName(NoticeDto::getTenantId), villageDto.getTenantId());
        }

        request.put(LambdaUtil.getFieldName(BannerDto::getStatus), SystemConstants.YES);
        int totalCount = bannerService.queryTotal(request.getParams());
        List<BannerDto> list = null;
        if (totalCount > 0) {
            list = bannerService.queryList(params);

        } else {
            list = new ArrayList<>();
        }
        Page<BannerDto> page = new Page<>(request.getPageNo(), request.getPageSize(), totalCount, list);
        return Response.<Page<BannerDto>>ok().wrap(page);
    }

}
